package com.wn.user.service.impl;

import com.wn.common.entity.Coordinate;
import com.wn.common.entity.MyRsp;
import com.wn.common.pojo.AreaVillage;
import com.wn.common.util.CoordinateUtil;
import com.wn.user.mapper.AreaVillageMappper;
import com.wn.user.param.AreaRsp;
import com.wn.user.service.AreaService;

import com.wn.user.util.MapValueComparator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @BelongsProject: shared_parking
 * @BelongsPackage: com.wn.user.service.impl
 * @Author: 廖刚
 * @CreateTime: 2020-06-18 11:15
 * @Description:
 */
@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private AreaVillageMappper areaVillageMappper;

    private final static Logger logger = LoggerFactory.getLogger(AreaServiceImpl.class);

    /**
     * 根据地区名得到地区下面所有的小区
     * @param aName
     * @return
     */
    @Override
    public List<AreaVillage> getVillageByAreaName(String aName) {
        List<AreaVillage> villageList = areaVillageMappper.getVillageByAreaName(aName);

        return villageList;
    }

    @Override
    public List<AreaVillage> getVillageByAId(String aid) {
        List<AreaVillage> villageList = areaVillageMappper.getVillageByAId(aid);

        return villageList;
    }

    /**
     * 根据名称获取坐标
     * @param areaVillage
     * @return
     */
    @Override
    public MyRsp getAreaByName(AreaVillage areaVillage) {
        AreaVillage areaVillage1 = areaVillageMappper.getAreaVillageByName(areaVillage);

        return  areaVillage1 != null ? MyRsp.success(areaVillage1):MyRsp.error().msg("查询失败");
    }

    /**
     * 查找所有的小区
     * @param coordinate
     * @return
     */
    @Override
    public MyRsp getCoordinate(Coordinate coordinate) {
        List<AreaVillage> villageList = areaVillageMappper.getAllVillage();
        //计算用户到每个小区的距离
        Map<String, Double> map = new HashMap<>();
        for(AreaVillage areaVillage:villageList){

            Coordinate coordinate1 = new Coordinate(areaVillage.getLongitude(), areaVillage.getLatitude());
            Double distance = CoordinateUtil.distanceByCoodinates(coordinate, coordinate1);
            map.put(areaVillage.getAId(),distance);
        }

        //对map集合找出距离最小的那个
        Set<Map.Entry<String, Double>> entries = map.entrySet();
        List<Map.Entry<String, Double>> entryList = new ArrayList<>(entries);

        Collections.sort(entryList,new MapValueComparator());
        logger.info("之后：{}",entryList);
        //最近的小区
        Map.Entry<String, Double> entry = entryList.get(0);
        //最近的小区id
        String vId = entry.getKey();
        //通过小区id获取所有地区按照最近的地区排第一个的顺序
        //首先通过小区id获取最近的地区对象
        AreaVillage areaVillage = areaVillageMappper.getAreaByVId(vId);
        //查询所有的除最近地区对象的地区列表
        List<AreaVillage> areaVillages = areaVillageMappper.getAllAreaBesideOne(areaVillage.getAId());
        areaVillages.add(0,areaVillage);
        //logger.info("areaVillages,{}",areaVillages);
        AreaRsp areaRsp = new AreaRsp(areaVillages, vId);
        return MyRsp.success(areaRsp);
    }

    /**
     * 动态查询地区小区表
     * @param areaVillage
     * @return
     */
    @Override
    public MyRsp getListByAreaVillage(AreaVillage areaVillage) {
        List<AreaVillage> areaVillageList = areaVillageMappper.getListByAreaVillage(areaVillage);
        return MyRsp.success(areaVillageList);
    }


}
